package com.csun.cmny.provider.device.watch.entity;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/// <summary>
    ///  下行语音文件配置包
    ///  关联上行确认 045
    ///  Tunneling voice file configuration package
    ///  Associated upstream confirmation 045
    /// </summary>
    public class C044
    {
        /// <summary>
        /// 数据起封包 @B#@ 
        /// Start Fixed value :@B#@ 
        /// </summary>
        public String HeadCode = "@B#@";
        /// <summary>
        /// 协议版本号
        /// Protocol version number
        /// </summary>
        public String Protocol = null;
        /// <summary>
        /// 功能代码识别
        /// Function code
        /// </summary>
        public final String FunCode = "044";
        /// <summary>
        /// 国际移动设备身份码
        /// International Mobile Equipment Identity
        /// </summary>
        public String IMEI = null;
        /// <summary>
        /// /语音类型  0 吃药  1喝水  2运动   3自定义
        /// Voice type 0 medication 1 drink water 2 exercise 3 custom
        /// </summary>
        public int VoiceType = 0;
        /// <summary>
        /// 语音格式 0：MP3
        /// Voice format    0: MP3
        /// </summary>
        public int VoiceFormat = 0;
        /// <summary>
        /// 10 进制表示 文件大小为 2040 个字节，那么这里填充“2040”
        /// Decimal indicates that the file size is 2040 bytes. Then fill in "2040" here.
        /// </summary>
        public int VoiceSize =1024;
        /// <summary>
        /// 如果语音文件分成 2 个包下发，那么这里填充 02
        /// If the voice file is delivered in 2 packages, it is filled here. 02
        /// </summary>
        public int PackageCount = 0;
        /// <summary>
        /// 分包索引   
        /// Packet Index 
        /// </summary>
        public int PackageIndex = 0;
        /// <summary>
        /// 分包大小  10进制 字节
        /// Packet size Decimal bytes
        /// </summary>
        public int PackageSize = 0;
        /// <summary>
        /// 语音数据 二进制数据流：byte[n]	
        /// Voice data binary stream：byte[n]	
        /// </summary>
        public  byte[] VoiceByte = null;
        /// <summary>
        /// 终端当前时间(年月日时分秒) yyyyMMddHHmmss
        /// The terminal current time ( format:yyyyMMddHHmmss)
        /// </summary>
        public String TerminalTime = null;
        /// <summary>
        /// 任务标识，用户自定义，方便回传标识
        /// Task identity, user custom, convenient return identification
        /// </summary>
        public String TaskId = null;
        /// <summary>
        /// 结尾封包 @E#@
        /// End packet @E#@
        /// </summary>
        public String FootCode = "@E#@";




        /// <summary>
        /// 初始化模型
        /// 包 1：@B#@|01|044|111112222233333|0|0|2040|2|1|1024|[	数	据	]| 20160715153805|0255BB90395C475E9D6155AF98B38387|@E#@
       /// </summary>
        /// <param name="cmdStr">命令代码</param>
        public C044(String cmdStr)
        {
        	String[] data = cmdStr.split("\\|");
            int dataLength = data.length;
            if (dataLength == 14 && "@B#@".equals(data[0]) && FunCode.equals(data[2]))
            {
                HeadCode = data[0];
                Protocol = data[1];
                IMEI = data[3];
                VoiceType = Integer.parseInt(data[4]);
                VoiceFormat = Integer.parseInt(data[5]);
                VoiceSize = Integer.parseInt(data[6]);
                PackageCount = Integer.parseInt(data[7]);
                PackageIndex = Integer.parseInt(data[8]);
                PackageSize = Integer.parseInt(data[9]);
                VoiceByte = data[10].getBytes();
                TerminalTime = data[11];
                TaskId = data[12];
                FootCode = data[13];
            }

        }
        public  String ToString()
        {
        	String rString = HeadCode + "|";
            rString += Protocol + "|";
            rString += FunCode + "|";
            rString += IMEI + "|";
            rString += VoiceType + "|" + VoiceFormat + "|" + VoiceSize + "|" + PackageCount + "|" + PackageIndex + "|" + PackageSize + "|" +
            		new String(VoiceByte) + "|";
            rString += TerminalTime + "|";
            rString += TaskId + "|";
            rString += FootCode;
            return rString;
        }
        
        /// <summary>
        /// 处理语音文件默认 1024大小分包
        /// </summary>
        /// <param name="VType">语音类型  0 吃药  1喝水  2运动   3自定义</param>
        /// <param name="IMEI">设备IMEI</param>
        /// <param name="voiceData">语音2进制流</param>
        /// <returns>返回 分包后的所有命令列表 </returns>
        @SuppressWarnings("null")
		public List<byte[]> ToListByte(int VType, String IMEI,byte[] voiceData) {
        	int voicePackLength = 1024;
            String TaskId = UUID.randomUUID().toString().replace("-", "").toLowerCase();

            List<byte[]> ListByte = null;
            //下发次数
            int voiceCount = voiceData.length % voicePackLength == 0
                ? voiceData.length / voicePackLength
                : voiceData.length / voicePackLength + 1;
            int voiceNowPageLength = voicePackLength;
            for (int voiceNowIndex = 1; voiceNowIndex <= voiceCount; voiceCount++) {
            	
            	
                if (voiceCount == voiceNowIndex)
                {
                    voiceNowPageLength = voiceData.length % voicePackLength == 0
                        ? voicePackLength
                        : voiceData.length % voicePackLength;
                }

            	 byte [] voiceIndexData=Arrays.copyOfRange(voiceData, voicePackLength*(voiceNowIndex-1), voiceNowPageLength);

                 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");  
                 long start = System.currentTimeMillis(); 
                 byte [] cmdbyte1 = ("@B#@|"+Protocol+"|044|" + IMEI +"|" + VType + "|"+ VoiceFormat +"|" + voiceData.length +  "|" + voiceCount + "|" + voiceNowIndex + "|" + voiceNowPageLength + "|").getBytes();
                 byte [] cmdbyte2 = ("|" + sdf.format(start) +"|" + TaskId + "|"+ FootCode).getBytes();
                 byte[] bt3 = new byte[cmdbyte1.length+cmdbyte2.length+voiceIndexData.length];  
                 System.arraycopy(cmdbyte1, 0, bt3, 0, cmdbyte1.length);  
                 System.arraycopy(voiceIndexData, 0, bt3, cmdbyte1.length, voiceIndexData.length);  
                 System.arraycopy(cmdbyte2, 0, bt3, voiceIndexData.length+cmdbyte1.length, cmdbyte2.length);  
                 ListByte.add(bt3);
            }
            return ListByte;


        }
    }




